home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Offworld CopyBits / main.c next >
Encoding:
C/C++ Source or Header  |  1994-07-06  |  4.7 KB  |  150 lines  |  [TEXT/KAHL]

  1. /* Offworld CopyBits Example project
  2.  *
  3.  * This code is public domain.
  4.  * written by Ken Worley 7/6/94
  5.  * AOL KNEworley
  6.  *
  7.  * This code creates a small color window and an offscreen graphics world
  8.  * (GWorld) the same size as the window.  It then uses the GWorld to smoothly
  9.  * animate a series of color icons with a horizontal line that moves down
  10.  * behind the icon.
  11.  */
  12.  
  13. #define kWindowID        128
  14. #define kStartIconID    128
  15. #define kFinishIconID    145
  16.  
  17. #include <QDOffscreen.h>    /* for GWorld routines */
  18.                             /* MacHeaders should get everything else needed */
  19.  
  20. void InitToolbox(void);
  21.  
  22. void InitToolbox()
  23. {
  24.     /* Initialize the Mac Toolbox Managers */
  25.     
  26.     InitGraf((Ptr) &qd.thePort);
  27.     InitFonts();
  28.     InitWindows();
  29.     InitMenus();
  30.     FlushEvents(everyEvent,0);
  31.     TEInit();
  32.     InitDialogs(0L);
  33.     InitCursor();
  34. }
  35.  
  36. void main( void )
  37. {
  38.     WindowPtr            myWindow;
  39.     PixMapHandle        thePixMap;
  40.     GWorldPtr            myOffscreen;
  41.     GDHandle            winDevice;
  42.     CGrafPtr            winPort;
  43.     RGBColor            myBlack;
  44.     RGBColor            myRed;
  45.     QDErr                myQDErr;
  46.     OSErr                myOSErr;
  47.     Rect                myRect;
  48.     
  49.     /* Initialize the Mac toolbox */
  50.         InitToolbox();
  51.  
  52.     /* Set up my RGB black */
  53.         myBlack.red = myBlack.green = myBlack.blue = 0;
  54.     
  55.     /* Get my color window from the resource, show it, and make it the current port */
  56.         myWindow = GetNewCWindow( kWindowID, NULL, (WindowPtr)(-1L) );
  57.         ShowWindow( myWindow );
  58.         SetPort( myWindow );
  59.         /* Save the window's port and device */
  60.             GetGWorld( &winPort, &winDevice );
  61.  
  62.     /* Create an offscreen graphics world the same size as my window */
  63.     /* Sending 0 as the second argument (the pixel depth) causes NewGWorld() to */
  64.     /* examine the global rectangle sent as the third argument and determine the */
  65.     /* deepest pixel map needed for that rectangle even if it crosses devices. */
  66.     
  67.         myQDErr = NewGWorld( &myOffscreen, 0, &(myWindow->portRect), NULL, NULL, 0L );
  68.     
  69.     /* If successfully created the graphics world, set it up to draw in */
  70.         if ( myQDErr == noErr )
  71.         {
  72.             /* Get the offscreen graphics world's pixel map handle */
  73.                 thePixMap = GetGWorldPixMap( myOffscreen );
  74.             /* Lock the pixels so they won't be moved while we're drawing */
  75.                 LockPixels( thePixMap );
  76.             /* Make my offscreen graphics world the current port */
  77.                 SetGWorld( myOffscreen, NULL );
  78.             /* set the foreground color to black */
  79.                 RGBForeColor( &myBlack );
  80.             /* Set the pen size to 1x4 */
  81.                 PenSize( 1, 4 );
  82.         }
  83.         else
  84.             myOffscreen = NULL;
  85.     
  86.     /* Draw in the offscreen graphics world and animate icon in window with CopyBits */
  87.     
  88.         if ( myOffscreen )    /* if graphics world is there */
  89.         {
  90.             Rect            tempRect;
  91.             short            x;
  92.             CIconHandle        myICON;
  93.             long            ignore;
  94.             
  95.             /* set up an icon-sized rectangle in the center of the window */
  96.                 tempRect = myOffscreen->portRect;
  97.                 SetRect( &tempRect, ((tempRect.right - tempRect.left)/2)-16,
  98.                             ((tempRect.bottom - tempRect.top)/2)-16,
  99.                             ((tempRect.right - tempRect.left)/2)+16,
  100.                             ((tempRect.bottom - tempRect.top)/2)+16);
  101.                 
  102.             /* loop through icons used for animation frames */
  103.                 for ( x=kStartIconID; x<=kFinishIconID; x++ )
  104.                 {
  105.                     /* Erase the whole offscreen rectangle */
  106.                         EraseRect( &(myOffscreen->portRect) );
  107.                     /* draw a horizontal line that moves down the window */
  108.                         MoveTo( myOffscreen->portRect.left,
  109.                                 tempRect.top + x - kStartIconID );
  110.                         LineTo( myOffscreen->portRect.right,
  111.                                 tempRect.top + x - kStartIconID );
  112.                     /* load the icon */
  113.                         myICON = GetCIcon( x );
  114.                     /* draw icon in offscreen graphics world */
  115.                         /* draw the icon */
  116.                             PlotCIcon( &tempRect, myICON );
  117.                     /* copy offscreen world to window */
  118.                         /* make window current port */
  119.                             SetGWorld( winPort, winDevice );
  120.                         /* set foreground color to black (affects CopyBits) */
  121.                             RGBForeColor( &myBlack );
  122.                         /* copy the image */
  123.                             CopyBits(
  124.                             &(((GrafPtr)myOffscreen)->portBits),//bitmap of gworld
  125.                             &(((GrafPtr)myWindow)->portBits),    //bitmap of window
  126.                             &(myOffscreen->portRect),            //portRect of gworld
  127.                             &(myWindow->portRect),                //portRect of window
  128.                             srcCopy, NULL );
  129.                         /* make offscreen world current port again */
  130.                             SetGWorld( myOffscreen, NULL );
  131.                     /* dispose of the icon */
  132.                         DisposeCIcon( myICON );
  133.                     /* Delay to slow animation (1/10 second) */
  134.                         Delay( 6, &ignore );
  135.                 } /* for */
  136.         } /* if */
  137.         
  138.     /* Unlock the offscreen world's pixel map */
  139.         if ( myOffscreen ) UnlockPixels( thePixMap );
  140.     /* Dispose of the offscreen graphics world */
  141.         if ( myOffscreen ) DisposeGWorld( myOffscreen );
  142.     /* make the window the current port */
  143.         SetGWorld( winPort, winDevice );
  144.     /* Write message in window */
  145.         MoveTo( myWindow->portRect.left + 5, myWindow->portRect.bottom - 14 );
  146.         DrawString( "\pclick to exit" );
  147.     /* Wait for a mouseDown to quit */
  148.         while ( !Button() ) {}
  149. }
  150.